#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author:Charmander

import os
import configparser
import codecs

proDir = os.path.split(os.path.realpath(__file__))[0]       #分割文件
configPath = os.path.join(proDir,'config.ini')

class ReadConfig:
    def __init__(self):
        fd = open(configPath)
        data = fd.read()

        if data[:3] == codecs.BOM_UTF8:
            data = data[3:]
            file = codecs.open(configPath,'w')
            file.writer(data)
            file.close()
        fd.close()
        #读取配置文件
        self.cf = configparser.ConfigParser()
        self.cf.read(configPath)

    #配置HTTP
    def get_http(self,name):
        value = self.cf.get('HTTP',name)
        return value

    #配置HEADERS
    def get_headers(self,name):
        value = self.cf.get('HEADERS',name)
        return value

    #定义HEADERS
    def set_headers(self,name,value):
        self.cf.set('HEADERS',name,value)
        with open(configPath,'w+') as f:
            self.cf.write(f)

    #配置URL
    def get_url(self,name):
        value = self.cf.get('URL',name)
        return value

    #配置EMAIL
    def get_email(self,name):
        value = self.cf.get('EMAIL',name)
        return value

    #配置DATABASE
    def get_db(self,name):
        value = self.cf.get('DATABASE',name)
        return value
